package com.aimee.servlet;

import com.aimee.dao.CourseDao;
import com.aimee.entity.Course;
import com.aimee.entity.User;
import com.google.gson.Gson;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/admin/course")
public class CourseServelt extends BaseServlet {

    /**
     * 向前端展示所有的题库数据
     *
     * @param request
     * @param response
     * @throws SQLException
     * @throws IOException
     */
    public void show(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        Gson gson = new Gson();
        CourseDao courseDao = new CourseDao();
        List<User> users = courseDao.show();
        String str = gson.toJson(users);
        String res = "{\n" +
                "  \"code\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"count\": 1000,\n" +
                "  \"data\": " + str + "\n" +
                "} ";
        PrintWriter out = response.getWriter();
        out.write(res);
//        String re = "{101,\"JavaOOP\",1,\"下列哪个选项不是Java面向对象的特点？\",\"admin\",\"May 23, 2020 6:18:03 PM\"}";
    }


    /**
     * 删除科目（题库）
     *
     * @param request
     * @param response
     * @throws SQLException
     * @throws IOException
     */
    public void del(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();

        String courseId = request.getParameter("id");
        int course_id = Integer.parseInt(courseId);
        CourseDao courseDao = new CourseDao();
        int i = courseDao.deleteCourse(course_id);
        out.write(i);
    }

    /**
     * 新增科目（题库）
     *
     * @param request
     * @param response
     * @throws SQLException
     * @throws IOException
     */
    public void add(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();

        String course_name = request.getParameter("name");
        String course_desc = request.getParameter("desc");
        System.out.println(course_name + "---" + course_desc);

        Course course = new Course();
        course.setCourse_name(course_name);
        course.setCourse_desc(course_desc);

        CourseDao courseDao = new CourseDao();

        int i = courseDao.addCourse(course);
        out.write(i);
    }

    /**
     * 题库编辑更新（科目）
     *
     * @param request
     * @param response
     * @throws SQLException
     * @throws IOException
     */
    public void update(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();

        String id = request.getParameter("id");

        String name = request.getParameter("name");
        String desc = request.getParameter("desc");

        System.out.println(id + "---" + name + "---" + desc);
        int course_id = Integer.parseInt(id);
        Course course = new Course(course_id, name, desc);
        System.out.println(course);
        CourseDao courseDao = new CourseDao();
        int i = courseDao.updateCourse(course);
        out.write(i);
    }


    /**
     * 查询所有的题库的名称
     *
     * @param request
     * @param response
     * @throws IOException
     */
    public void showCourseNames(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();

        CourseDao courseDao = new CourseDao();
        try {
            ArrayList list = courseDao.showCourseNames();
            for (int i = 0; i < list.size(); i++) {
                System.out.println(list.get(i).toString());
            }

            Gson gson = new Gson();
            String json = gson.toJson(list);
            System.out.println(json);
            out.write(json);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        out.close();
    }


}
